package inv.stask.ui;

import inv.stask.beans.Task;
import inv.stask.beans.Task.Priority;

import java.util.ArrayList;
import java.util.List;

import javax.swing.table.AbstractTableModel;

public class TaskListTableModel extends AbstractTableModel {
	private static final long serialVersionUID = -438394208323423960L;
	private List<Task> taskList;
	private int columnCount = 4;

	public int getColumnCount() {
		return columnCount;
	}

	public int getRowCount() {
		return taskList.size();
	}
	
	@Override
	public String getColumnName(int column) {
		String name	= "";
		switch(column){
		case 0: name = "Due";
				break;
		case 1: name = "Task";
				break;
		case 2: name = "Priority";
				break;
		case 3: name = "Done";
				break;
		}
		return name;
	}

	public Object getValueAt(int rowIndex, int columnIndex) {
		Task task	= taskList.get(rowIndex);
		if(columnIndex == 0){
			return task;
		}
		else
			if(columnIndex == 1)
				return task.getName();
			else
					if(columnIndex == 2)
						return task.getPriority();
					else
						if(columnIndex == 3)
							return task.getDone();
		
		return null;
	}
	
	 @SuppressWarnings("unchecked")
	public Class getColumnClass(int c) {
	        return getValueAt(0, c).getClass();
	}

	public List<Task> getTaskList() {
		return taskList;
	}

	public void setTaskList(List<Task> taskList) {
		if(taskList != null)
			this.taskList = taskList;
		else
			this.taskList = new ArrayList<Task>();
	}
	
	@Override
	public boolean isCellEditable(int rowIndex, int columnIndex) {
		return true;
	}
	
	@Override
	public void setValueAt(Object value, int rowIndex, int columnIndex) {
		Task task	= taskList.get(rowIndex);
		if(columnIndex == 0){/*do nothing*/}
		else
			if(columnIndex == 1)
				task.setName((String)value);
			else
				if(columnIndex == 2){
					task.setPriority((Priority)value);
					fireTableRowsUpdated(rowIndex, rowIndex);
				}
				else
					if(columnIndex == 3){
						task.setDone((Boolean)value);
						fireTableRowsUpdated(rowIndex, rowIndex);
					}
	}


}
